<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 请求数据接口
 * @param $url
 * @param null $postFields
 * @param array $header
 * @return mixed
 * @throws Exception
 */
function curl_request($url, $postFields = null, $header = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FAILONERROR, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //https 请求
    if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    if ($postFields) {
        array_push($header, "Content-Length:" . strlen(json_encode($postFields)));
        array_push($header, 'Content-Type:application/json');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        throw new Exception(curl_error($ch), 0);
    } else {
        $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (200 !== $httpStatusCode) {
            throw new Exception($response, $httpStatusCode);
        }
    }
    curl_close($ch);
    return json_decode($response, true);
}

/**
 * 发送消息
 * @param $content
 * @return mixed
 * @throws Exception
 */
function commonSendMessage($content)
{
    $url   = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e14ae630-5951-41e7-8f85-2bc742d4f6be';
    $param = [
        "msgtype" => "text",
        "text"    => ["content" => $content]
    ];
    return curl_request($url, $param);
}
